Expand description
Test and assert log statements
This crate makes it easy to test and assert log messages. Logging can be a crucial part of any API, and ensuring log messages are tested is an important in preventing accidental regressions.
§Constraints
logtest
uses a per-binary message queue to store messages in. This
enables it to work in any async setting, and captures the global ordering of
messages for the code being tested.
Because Rust spawns per-test threads during integration
testing,
when testing log statements it’s recommended to only have a single
#[test]
block per test file. That prevents possible race conditions from
tests from running in parallel.
A convention we recommend is adding a test/log.rs
file that contains a
single #[test]
block that drives all log assertions. Splitting the code
can be done by calling out to regular fuctions from the #[test]
function.
§Examples
use logtest::Logger;
// Start the logger.
let mut logger = Logger::start();
// Log some messages.
log::info!("hello");
log::info!("world");
// The messages are now available from the logger.
assert_eq!(logger.pop().unwrap().args(), "hello");
assert_eq!(logger.pop().unwrap().args(), "world");
Structs§
- The test logger.
- The “payload” of a log message.
Functions§
- Create a new instance of
Logger
and start listening for events.